package com.ly.dingtalk.service.chat.impl;

import com.aliyun.dingtalkim_1_0.Client;
import com.aliyun.dingtalkim_1_0.models.ChatIdToOpenConversationIdHeaders;
import com.aliyun.dingtalkim_1_0.models.ChatIdToOpenConversationIdResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.dingtalk.api.request.OapiChatCreateRequest;
import com.dingtalk.api.request.OapiChatGetRequest;
import com.dingtalk.api.request.OapiChatUpdateRequest;
import com.dingtalk.api.response.OapiChatCreateResponse;
import com.dingtalk.api.response.OapiChatGetResponse;
import com.dingtalk.api.response.OapiChatUpdateResponse;
import com.ly.dingtalk.common.exception.DingApiException;
import com.ly.dingtalk.component.application.IDingApp;
import com.ly.dingtalk.constant.DingUrlConstant;
import com.ly.dingtalk.service.AbstractDingNewApi;
import com.ly.dingtalk.service.chat.IDingChatHandler;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

/**
 * 群组相关
 *
 * @author wangzhen
 */
@Slf4j
public class DingChatHandler extends AbstractDingNewApi implements IDingChatHandler {

    public DingChatHandler(IDingApp app) {
        super(app);
    }

    /**
     * @since 1.0
     */
    @Override
    public String getOpenConversationIdByChatId(@NonNull String chatId) {
        ChatIdToOpenConversationIdHeaders chatIdToOpenConversationIdHeaders = new ChatIdToOpenConversationIdHeaders();
        chatIdToOpenConversationIdHeaders.xAcsDingtalkAccessToken = accessToken();
        final ChatIdToOpenConversationIdResponse response = execute(
                Client.class,
                client -> {
                    try {
                        return client.chatIdToOpenConversationIdWithOptions(chatId, chatIdToOpenConversationIdHeaders, new RuntimeOptions());
                    } catch (Exception e) {
                        log.error("根据chatId获取getOpenConversationId", e);
                        throw new DingApiException("根据chatId获取getOpenConversationId", e);
                    }
                },
                () -> "根据chatId获取getOpenConversationId"
        );
        return response.getBody().getOpenConversationId();
    }

    /**
     * @since 1.0
     * @Description: 创建群
     * @see com.ly.dingtalk.service.chat.IDingChatHandler#createChat(com.dingtalk.api.DefaultDingTalkClient)
     */
    @Override
    public OapiChatCreateResponse createChat(OapiChatCreateRequest req) {
        return execute(
                DingUrlConstant.CHAT.CREATE_CHAT,
                req,
                () -> "创建群"
        );
    }

    /**
     * @since 1.0
     */
    @Override
    public OapiChatUpdateResponse updateChat(OapiChatUpdateRequest req) {
        return execute(
                DingUrlConstant.CHAT.UPDATE_CHAT,
                req,
                () -> "更新群"
        );
    }
    
    /**
     * @since 1.0
     */
    @Override
    public OapiChatGetResponse getChatById(@NonNull String chatId) {
        OapiChatGetRequest req = new OapiChatGetRequest();
        req.setChatid(chatId);
        req.setHttpMethod("GET");
        return execute(
                DingUrlConstant.CHAT.GET_CHAT_BY_ID,
                req,
                () -> "更新群"
        );
    }
}
